<?php
/**
 * Created by PhpStorm.
 * User: liuwei
 * Date: 2022/12/16
 * Time: 14:13
 */

namespace App\Services\Company\Models;

use App\Services\Business\Models\Course;
use App\Services\Business\Models\MediaVideo;
use App\Services\Business\Models\Role;
use App\Services\Member\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Services\Member\Models\Team;
class Process extends Model
{

    use HasFactory;


    protected $table = 'company_process';

    protected $fillable = [
        'id', 'user_id', 'title','company_id'
    ];


    protected $appends = [
        'expired_days', 'is_expired'
    ];

    public function setProfessionTagAttribute($professionTag)
    {
        if (is_array($professionTag) && count($professionTag) > 0 ){

            $this->attributes['profession_tag']= implode("|", $professionTag);
        }else{
            $this->attributes['profession_tag'] ='';
        }

    }
    public function getProfessionTagAttribute($professionTag)
    {
        if (strpos($professionTag, "|") !== false){
            return  explode("|", $professionTag);
        }elseif (strlen($professionTag)>0){

            return  [$professionTag];
        }
        else{
            return  $professionTag;
        }

    }

    public function getExpiredDaysAttribute()
    {
        return  Carbon::now()->diffInDays($this->expired_at,false);
    }

    public function getIsExpiredAttribute()
    {
        if (is_null($this->expired_at)) {

            return true;
        }

        return  Carbon::now()->diffInDays($this->expired_at,false) <= 0;
    }

    public function users()
    {
        return $this->belongsToMany(
            CompanyAdministrator::class,
            'company_administrator_relation',
            'company_id',
            'administrator_id'
        );
    }
    /**
     *店铺所属平台角色.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function role()
    {
        return $this->belongsTo(CompanyRole::class, 'role_id');
    }

    public function videos()
    {
        return $this->hasMany(MediaVideo::class, 'company_id')->where('is_open',1);
    }

    public function members()
    {
        return $this->belongsToMany(User::class, 'shop_user_role_relation','company_id','user_id');
    }
    /**
     * @return \illuminate\database\eloquent\relations\hasmany
     */
    public function roles()
    {
        return $this->hasmany(Role::class, 'company_id');
    }

    /**
     * @return \illuminate\database\eloquent\relations\hasmany
     */
    public function courses()
    {
        return $this->hasmany(Course::class, 'company_id');
    }


    public function  owner(){

        return $this->belongsTo(CompanyAdministrator::class, 'user_id','id');

    }


    public function roleCourses()
    {

        return $this->belongsToMany(Course::class, 'user_role_course','company_id','course_id')->withPivot('user_id');

    }

    public function teams()
    {
        return $this->hasmany(Team::class, 'company_id');
    }








}